package com.my.zl.console.service.autocode.impl;

import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.my.zl.common.dto.ReqDTO;
import com.my.zl.common.dto.advancedQuery.PageQueryDTO;
import com.my.zl.common.enums.resp.GlobalEnum;
import com.my.zl.common.exception.BizException;
import com.my.zl.common.utils.CopyUtil;
import com.my.zl.console.common.constant.AutocodeC;
import com.my.zl.console.common.constant.AutocodeKeyC;
import com.my.zl.console.dao.autocode.ZlEntityAttrMapper;
import com.my.zl.console.entity.autocode.ZlEntityAttr;
import com.my.zl.console.po.autocode.entityAttr.ZlEntityAttrVo;
import com.my.zl.console.dto.autocode.entityAttr.ReqEntityAttrAddDTO;
import com.my.zl.console.dto.autocode.entityAttr.ReqEntityAttrDelDTO;
import com.my.zl.console.dto.autocode.entityAttr.ReqEntityAttrEditDTO;
import com.my.zl.console.dto.autocode.entityAttr.ReqEntityAttrQueryDTO;
import com.my.zl.console.dto.autocode.entityAttr.ReqEntityAttrDetailDTO;
import com.my.zl.console.dto.autocode.entityAttr.ReqEntityAttrStateDTO;
import com.my.zl.console.dto.autocode.entityAttr.ResEntityAttrAddDTO;
import com.my.zl.console.dto.autocode.entityAttr.ResEntityAttrDelDTO;
import com.my.zl.console.dto.autocode.entityAttr.ResEntityAttrEditDTO;
import com.my.zl.console.dto.autocode.entityAttr.ResEntityAttrQueryDTO;
import com.my.zl.console.dto.autocode.entityAttr.ResEntityAttrDetailDTO;
import com.my.zl.console.dto.autocode.entityAttr.ResEntityAttrStateDTO;
import com.my.zl.console.dto.autocode.entityAttr.ResEntityAttrVO;
import com.my.zl.console.dto.autocode.entityAttr.ResEntityAttrVO2;
import com.my.zl.console.service.autocode.IEntityAttrService;
import com.my.zl.console.service.base.BaseManager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;

/**
 * 实体属性配置
 * @author ZL
 */
@Service
public class EntityAttrServiceImpl extends BaseManager implements IEntityAttrService {

    @Autowired
    private ZlEntityAttrMapper entityAttrMapper;

    @Override
    public ResEntityAttrQueryDTO queryEntityAttrList(ReqEntityAttrQueryDTO reqInfo,PageQueryDTO page) throws Exception{
        // TODO Auto-generated method stub
        ResEntityAttrQueryDTO resDTO = BeanUtil.copyProperties(page, ResEntityAttrQueryDTO.class);
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqInfo);
        
        pageHandle(page);//处理分页数据
        //当效率不行时自行计数，或完全自行分页，先查询总数，如果为0不用再次查询了；开启计数：true是,false否
        //临近第一条SQL查询语句自动分页,如果未消费时(即后面没有触发SQL查询)则需主动调用pageHp.clear()清理
        Page<Object> pageHp = PageHelper.startPage(page.getPage(), page.getLimit(),page.getSortorder());
        List<ZlEntityAttrVo> list = entityAttrMapper.selectListPage(paramMap);
        resDTO.setData(CopyUtil.copyList(list, ResEntityAttrVO2.class));
        resDTO.setCount(pageHp.getTotal());
        
        return resDTO;
    }
    
    @Override
    public ResEntityAttrDetailDTO queryEntityAttrDetail(ReqEntityAttrDetailDTO reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ResEntityAttrDetailDTO res = new ResEntityAttrDetailDTO();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqInfo);
        ZlEntityAttr view = entityAttrMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(view, res);
        return res;
    }
    
    @Override
    public ResEntityAttrAddDTO insertEntityAttrInfo(ReqDTO<ReqEntityAttrAddDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqEntityAttrAddDTO reqData = reqInfo.getReqData();
        ResEntityAttrAddDTO res = new ResEntityAttrAddDTO();
        Instant time = getSystemTime().getUtcTime();
        
        ZlEntityAttr record = new ZlEntityAttr();
        BeanUtil.copyProperties(reqData, record);
        
        initCreateInfo(record, reqInfo, time);
        record.setEntityAttrId(IdUtil.simpleUUID());
        record.setDataStatus(AutocodeC.DataStatus.ENABLE.getCode());//状态
        
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("entityAttrId",record.getEntityAttrId());
        //检查主键是否冲突
        if(entityAttrMapper.countExistKey(paramMap)>0){
            throw new BizException(GlobalEnum.DUPLICATE_PRIMARY_KEY);
        }
        
        entityAttrMapper.insertSelective(record);
        
        ZlEntityAttr keyInfo = entityAttrMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResEntityAttrEditDTO updateEntityAttrInfo(ReqDTO<ReqEntityAttrEditDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqEntityAttrEditDTO reqData = reqInfo.getReqData();
        ResEntityAttrEditDTO res = new ResEntityAttrEditDTO();
        Instant time = getSystemTime().getUtcTime();
        
        ZlEntityAttr record = new ZlEntityAttr();
        BeanUtil.copyProperties(reqData, record);
        
        initUpdateInfo(record, reqInfo, time);
        
        entityAttrMapper.updateByPrimaryKeySelective(record);
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("entityAttrId",record.getEntityAttrId());
        ZlEntityAttr keyInfo = entityAttrMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResEntityAttrDelDTO deleteEntityAttrInfo(ReqDTO<ReqEntityAttrDelDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqEntityAttrDelDTO reqData = reqInfo.getReqData();
        ResEntityAttrDelDTO res = new ResEntityAttrDelDTO();
        Instant time = getSystemTime().getUtcTime();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqData);
        entityAttrMapper.deleteByMap(paramMap);
        
        return res;
    }
    
    @Override
    public ResEntityAttrStateDTO updateEntityAttrState(ReqDTO<ReqEntityAttrStateDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqEntityAttrStateDTO reqData = reqInfo.getReqData();
        ResEntityAttrStateDTO res = new ResEntityAttrStateDTO();
        Instant time = getSystemTime().getUtcTime();
         
        ZlEntityAttr record = new ZlEntityAttr();
        BeanUtil.copyProperties(reqData, record);
        initUpdateInfo(record, reqInfo, time);
        entityAttrMapper.updateByPrimaryKeySelective(record);
         
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("entityAttrId",record.getEntityAttrId());
        ZlEntityAttr keyInfo = entityAttrMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
}